import instance from '../utils/request'
import { Message } from 'element-ui'

// User修改提交
async function editDormitory (dormitory) {
  return await instance({
    url: 'dormitory',
    method: 'put',
    data: dormitory
  }).then(res => {
    return res
  }).catch(() => {
    Message.error('修改失败,服务器错误!')
  })
}
// User修改提交
async function addDormitory (dormitory) {
  return await instance({
    url: 'dormitory',
    method: 'post',
    data: dormitory
  }).then(res => {
    return res
  }).catch(() => {
    Message.error('修改失败,服务器错误!')
  })
}

// 得到所有宿舍
async function getDormitory (page, limit, search) {
  return await instance.get('/dormitory/getDormitory?page=' + page + '&limit=' + limit + '&dormitoryName=' + search).then(data => {
    return data
  }).catch(() => {
    Message.error('没有权限,请先登录')
  })
}

// 得到所有宿舍
async function getAll () {
  return await instance.get('/dormitory/all/list').then(data => {
    return data
  }).catch(() => {
    Message.error('没有权限,请先登录')
  })
}

// 得到所有宿舍ID
async function getIds () {
  return await instance.get('/dormitory').then(data => {
    return data
  }).catch(() => {
    Message.error('没有权限,请先登录')
  })
}

// 删除宿管
async function deleteDormitory (dormitoryId) {
  return await instance.delete('/dormitory/' + dormitoryId).then(res => {
    return res
  }).catch(() => {
    Message.error('删除失败,服务器错误!')
  })
}

// 删除宿管
async function deleteAllDormitory (dormitoryId) {
  return await instance.delete('/dormitory/all/' + dormitoryId).then(res => {
    return res
  }).catch(() => {
    Message.error('删除失败,服务器错误!')
  })
}

export default {
  editDormitory,
  getDormitory,
  getAll,
  deleteDormitory,
  deleteAllDormitory,
  addDormitory,
  getIds
}
